package com.hotel.po.mapper;

import com.hotel.common.utils.DBTools;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class ReviewMapper {

    public List<Map<String, String>> findByHotelId(Long hotelId) {
        String base = "SELECT id, hotel_id AS hotelId, user_id AS userId, rating, content, created_at AS createdAt FROM review";
        return hotelId == null
                ? DBTools.queryWithBusiness(base + " ORDER BY id DESC")
                : DBTools.queryWithBusiness(base + " WHERE hotel_id=? ORDER BY id DESC", hotelId);
    }

    public List<Map<String, String>> findAll() {
        String sql = "SELECT r.id, r.hotel_id AS hotelId, r.user_id AS userId, r.rating, r.content, r.created_at AS createdAt, " +
                    "h.name AS hotelName " +
                    "FROM review r " +
                    "LEFT JOIN hotel h ON r.hotel_id = h.id " +
                    "ORDER BY r.id DESC";
        return DBTools.queryWithBusiness(sql);
    }

    public Long insert(Long hotelId, Long userId, Integer rating, String content) {
        String sql = "INSERT INTO review(hotel_id,user_id,rating,content) VALUES(?,?,?,?)";
        DBTools.updateWithBusiness(sql, hotelId, userId, rating, content);
        String id = DBTools.querySingle("SELECT LAST_INSERT_ID() AS id");
        return Long.valueOf(id);
    }

    public void delete(Long id) {
        String sql = "DELETE FROM review WHERE id=?";
        DBTools.updateWithBusiness(sql, id);
    }
}


